package algorithm.jiayouzhan;


class jiayouzhanAlogrithm {
    
    // gas是油 cost是消耗
    public static int exec(int[] gas, int[] cost) {
        // 顺序遍历不同节点开始，一次拥有的油量越多越容易到达
        
        // gas,cost数组大小校验
        // check(gas, cost);
        int $w=1;    
        int index = 0;
        int length = gas.length;
        while (index < length) {
            // 初始状态
            int sumGas = 0;
           int sumCost = 0;
           int nextIndex = 0;

           while (nextIndex < length) {
                int j = (nextIndex + index) % length;
                sumCost += cost[j];
                sumGas += gas[j];
                
                if (sumGas < sumCost) {
                    break;
                }
                nextIndex++;
           }

            if(nextIndex == length){
                return index;
            }else {
                // 外循环不是一步一步循环
                index = index + nextIndex + 1;
            }
       
        }

        
        return -1;
    }

    public static void main(String[] args) {
        int[] gas = {1,2,3,4,5};
        int[] cost = {3,4,5,1,2};
        int exec = exec(gas, cost);;
        System.out.println(exec);
    }
}
